package auto.test.runcase.ems_case;

import static org.testng.Assert.assertEquals;

import org.apache.log4j.Logger;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Listeners;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

import auto.test.base.BaseCase;
import auto.test.base.BaseHandle;
import auto.test.handle.CheckdrawsIMGHandle;
import auto.test.listener.TestNGListenerScreen;
import auto.test.page.CheckviewPage;
import auto.test.service.Addboth;
import auto.test.service.LoginService;
import auto.test.util.AssertUtil;
import auto.test.util.DownloadUtil;


@Listeners(TestNGListenerScreen.class)

public class CheckviewCase extends BaseCase{
	
		Logger logger=Logger.getLogger(CheckviewCase.class);
		public WebDriver driver;
		public HomeHandle homeHandle;
		public LoginHandle loginHandle;
		public BaseHandle baseHandle;
		public CheckdrawsIMGHandle checkdrawsIMGHandle;
		public CheckviewPage checkdrawPage;
		public PublicHandle publicHandle;
		public BothdetailHandle bothdetailHandle;
		AssertUtil assertu=new AssertUtil(driver);
	Addboth add=new Addboth();
	String bothnum=add.Getbothnum();

		  @Parameters({"browser","exhibitionname","user","password"})
		  @BeforeClass
		  public void beforeClass(String browser,String exhibitionname,String user,String password) {
			  driver=GetDriver(browser);
			  max();
			  LoginService login=new LoginService();
			  login.Login(driver,exhibitionname,user,password);
			  homeHandle= new HomeHandle(driver);
		 		 bothdetailHandle=new BothdetailHandle(driver);
				 publicHandle=new PublicHandle(driver);
				 checkdrawsIMGHandle=new CheckdrawsIMGHandle(driver);
				  checkdrawPage=new CheckviewPage(driver);
				  publicHandle=new PublicHandle(driver);

		  }
		  
		  @BeforeMethod
		  public void beforemethod() {

				 
		  }



	@Parameters({"sthz_filetype"})
	@Test(groups="test_sucess",description="auto_st_001_下载审图回执",timeOut = 30000)
	public void download_sthz(final String sthz_filetype) throws Exception {
		homeHandle.Clickfunctionbutton("functionbutton_st");
		checkdrawsIMGHandle.ClickapplyTAB(1);

		int j=checkdrawPage.getdownload_sthzelement().size();
		for(int i=0;i<checkdrawPage.getpageelement().size();i++) {
			if(j==0) {
				checkdrawPage.getpageelement().get(i+1).click();
				j=checkdrawPage.getdownload_sthzelement().size();
			}else {

				new Thread(new Runnable() {
					@Override
					public void run() {
						checkdrawsIMGHandle.Clickdownload(0);
					}
				}).start();
				final String downloadsPath=DownloadUtil.getdownloadconfig("downloadsPath");

				String filename= DownloadUtil.getDownloadedDocumentName(downloadsPath, sthz_filetype);
				boolean flag= DownloadUtil.isFileDownloaded_Ext(downloadsPath, filename);

				System.out.println(flag+"----------------------------------------------");
				assertEquals(flag, true);
				break;
			}
			if(i>checkdrawPage.getpageelement().size()) {

			}
		}
	}


	@Test(groups="test_sucess",description="auto_st_002_添加审图")
      public void add() throws Exception {

		 Thread.sleep(2000);
 		 add.newboth(driver,bothnum); 
 		publicHandle.Clickreturnhome();
 		homeHandle.Clickfunctionbutton("functionbutton_st");
 		checkdrawsIMGHandle.ClickAddbutton();
 		checkdrawsIMGHandle.intputboth(bothnum);
 		checkdrawsIMGHandle.clicksearchboth();
 		checkdrawsIMGHandle.clickfirststep();
 		for (WebElement str :checkdrawPage.basicinformation()) {
 	    str.sendKeys("100");
 		}
 		checkdrawsIMGHandle.clicksecstep();
 		checkdrawsIMGHandle.sendbuilderinput("91110108585852240Q", 0);
 		checkdrawsIMGHandle.sendbuilderinput("553187951@163.com",8);
		checkdrawsIMGHandle.sendbuilderinput("666666",9);

		//	checkdrawsIMGHandle.sendbuilderinput("666666",9);
		for(WebElement ele:checkdrawPage.builder_information()) {
 			if(ele.getAttribute("value").toString().length()!=0) {
 				System.out.println(ele.getAttribute("value"));
 			}else {
 				ele.sendKeys("13129562261");
 			}
 		}
 		checkdrawsIMGHandle.clickthirdstep();
 		checkdrawsIMGHandle.uploadimg_input();
		Thread.sleep(1000);
		checkdrawsIMGHandle.clickcommitst();
		if(checkdrawPage.cp_dtysxz().size()>0){
			checkdrawsIMGHandle.clickcp_submitdt();
		}else{
			logger.info("该展会无地毯配置！！！！！！！");
		}
		//assertEquals(checkdrawPage.assertcomit().getText(),"报图完成") ;
		assertu.assertContains(checkdrawPage.assertcomit().getText(),"完成");
      }

	@Test(groups="test_exception",description="auto_st_003_重复展位审图_未审核",dependsOnMethods = {"add"})
	public void repeat_add() throws Exception {
		//String bothnum="auto_cfsh";
		homeHandle.Clickfunctionbutton("functionbutton_st");
		checkdrawsIMGHandle.ClickAddbutton();
		checkdrawsIMGHandle.intputboth(bothnum);
		checkdrawsIMGHandle.clicksearchboth();
	//	assertEquals(checkdrawPage.assertallowadd().getText(),"该展位已通过审图，请勿重复提交");
		assertu.assertContains(checkdrawPage.assertallowadd().getText(),"当前展位已报图");
	}

	@Test(groups="test_exception",description="auto_st_004_不存在的展位审图")
	public void notfound_add() throws Exception {
		String bothnum="auto_notfound";
		homeHandle.Clickfunctionbutton("functionbutton_st");
		checkdrawsIMGHandle.ClickAddbutton();
		checkdrawsIMGHandle.intputboth(bothnum);
		checkdrawsIMGHandle.clicksearchboth();
		assertu.assertContains(checkdrawPage.assertnotfound().getText(),"没有当前展位");

	}
	@Test(groups="test_exception",description="auto_st_005_标摊展位审图")
	public void wrongtype_add() throws Exception {
		String bothnum="auto_bt";
		homeHandle.Clickfunctionbutton("functionbutton_st");
		checkdrawsIMGHandle.ClickAddbutton();
		checkdrawsIMGHandle.intputboth(bothnum);
		checkdrawsIMGHandle.clicksearchboth();
		assertu.assertContains(checkdrawPage.assertnotfound().getText(),"没有当前展位");

	}


	@Test(groups="test_sucess",description="auto_st_006_撤销审图",dependsOnMethods = {"add"})
		public void cancel(){
		homeHandle.Clickfunctionbutton("functionbutton_st");
		checkdrawsIMGHandle.ClickapplyTAB(1);
		//checkdrawsIMGHandle.Clickcancel_checkbutton(0);
		checkdrawsIMGHandle.Clickcancelbutton();
		checkdrawsIMGHandle.clickcanclesure();
		assertEquals(checkdrawPage.assertcancel().getText(),"取消成功");

		  }

	@Test(groups="test_sucess",description="auto_st_007_群二维码是否加载")
	public void qrcode_load() {
		homeHandle.Clickfunctionbutton("functionbutton_st");
		checkdrawsIMGHandle.Clickqrcodebutton();
		checkdrawPage = new CheckviewPage(driver);
		//Thread.sleep(20000);
		WebElement image = checkdrawPage.getqrcodeimgelement();
		boolean isVisible = this.IsImageVisible(driver, image);
		assertEquals(isVisible, true);

	}

	@Test(groups="test_exception",description="auto_st_008_重复展位审图_已通过")
	public void repeat_add_pass() throws Exception {
		String bothnum="test_bind";
		homeHandle.Clickfunctionbutton("functionbutton_st");
		checkdrawsIMGHandle.ClickAddbutton();
		checkdrawsIMGHandle.intputboth(bothnum);
		checkdrawsIMGHandle.clicksearchboth();
		//	assertEquals(checkdrawPage.assertallowadd().getText(),"该展位已通过审图，请勿重复提交");
		assertu.assertContains(checkdrawPage.cp_assertpass().getText(),"该展位已通过审图，请勿重复提交");
	}



	@Test(groups="test_exception",description="auto_st_009_重复展位审图_未通过")
	public void repeat_add_fail() throws Exception {
		String bothnum="M00006";
		homeHandle.Clickfunctionbutton("functionbutton_st");
		checkdrawsIMGHandle.ClickAddbutton();
		checkdrawsIMGHandle.intputboth(bothnum);
		checkdrawsIMGHandle.clicksearchboth();
		//	assertEquals(checkdrawPage.assertallowadd().getText(),"该展位已通过审图，请勿重复提交");
		assertu.assertContains(checkdrawPage.cp_assertfail().getText(),"当前展位已报图");
	}

	@AfterMethod
		public void AfterMethod() throws InterruptedException {
			publicHandle.Clickreturnhome();
			Thread.sleep(2000);

	}

		

	


	  
	  @AfterClass
	  public void afterClass() {
		  driver.quit();
	  }
		
	}
	


